package com.hhxy.servlet;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;

/**
 * @author ghp
 * @date 2022/9/8
 */
@WebServlet("/servletContextTestServlet")
public class ServletContextTestServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1、获取ServletContext对象
        ServletContext context = this.getServletContext();
        //2、获取所有初始化配置的参数列表
        Enumeration<String> params = context.getInitParameterNames();
        //3、遍历参数列表，同时打印各参数对应的值
        while(params.hasMoreElements()){
            String name = params.nextElement();
            String value = context.getInitParameter(name);
            System.out.println(name+"="+value);
        }
        /*实现多个数据的共享*/
        context.setAttribute("data"," This servlet save data");
    }
}